package com.wzx.ptplatform.controller.admin.set;

import com.wzx.ptplatform.domain.Industry;
import com.wzx.ptplatform.service.IndustryService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员其他模块——行业管理")
@RestController
public class AdminSetIndustryController {

    final IndustryService industryService;

    public AdminSetIndustryController(IndustryService industryService) {
        this.industryService = industryService;
    }

    @RequestMapping("admin_set_industry_message_list")
    public LayuiPager<Industry> adminSetIndustryMessageList(
            String industryName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Industry industry = new Industry();
        if (industryName != null && !"".equals(industryName)) {
            industry.setIndustryName(industryName);
        }
        Page<Industry> pager = industryService.findList(industry, page - 1, limit);
        LayuiPager<Industry> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_set_industry_message_addView")
    public ModelAndView adminSetIndustryMessageAddView() {
        return new ModelAndView("admin/set/industry_message_add");
    }

    @GetMapping("admin_set_industry_message_editView")
    public ModelAndView adminSetIndustryMessageEditView(String id, Model model) {
        model.addAttribute("industry", industryService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/set/industry_message_edit");
    }

    @PostMapping("admin_set_industry_message_save_or_edit")
    public Map<String, Object> adminSetIndustryMessageSaveOrEdit(Industry industry) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (industryService.saveOrEdit(industry)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_set_industry_message_delete")
    public Map<String, Object> adminSetIndustryMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (industryService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_set_industry_message_delete_select")
    public Map<String, Object> adminSetIndustryMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    industryService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
